package com.zts.modules.sys.mapper;

import com.google.common.base.Strings;
import com.zts.modules.sys.entity.Code;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**
 * Created by lzk on 2017/6/16.
 */
public class CodeSqlProvider {
    public String get(final Long id) {
        return new SQL() {
            {
                SELECT("u.*");
                FROM(" sys_code u  ");
                WHERE(" u.id = #{id} ");
            }
        }.toString();
    }

    public String getByEntity(final Code code) {
        return new SQL() {
            {
                SELECT("u.*");
                FROM(" sys_code u  ");
                //通过条件 判断是否需要更新该字段
                if (!Strings.isNullOrEmpty(code.getCataCode())) {
                    WHERE("u.cata_code = #{cataCode}");
                }
                if (!Strings.isNullOrEmpty(code.getCataName())) {
                    WHERE("u.cata_name = #{cataName}");
                }
                if (!Strings.isNullOrEmpty(code.getSubCode())) {
                    WHERE("u.sub_code = #{subCode}");
                }
                if (!Strings.isNullOrEmpty(code.getSubCodeName())) {
                    WHERE("u.sub_code_name = #{subCodeName}");
                }
                if (!Strings.isNullOrEmpty(code.getStatus())) {
                    WHERE("u.status = #{status}");
                }

                if (null != code.getId() && code.getId() > 0) {
                    WHERE("u.id  = #{id}");
                }

                ORDER_BY(" u.id ");
            }
        }.toString();
    }

    public String insert(final Code code) {
        return new SQL() {{
            INSERT_INTO("sys_code");
            VALUES("cata_code", "#{cataCode}");
            VALUES("cata_name", "#{cataName}");
            VALUES("sub_code", "#{subCode}");
            VALUES("sub_code_name", "#{subCodeName}");
            VALUES("create_time", "now()");
            if (!Strings.isNullOrEmpty(code.getStatus())) {
                VALUES("status", "#{status}");
            } else {
                VALUES("status", "1");
            }
        }}.toString();
    }

    public String getCodeValue(final Map<String, Object> para) {
        return new SQL() {
            {
                SELECT("u.*");
                FROM(" sys_code u  ");
                WHERE(" u.cata_code = #{cataCode}");
                WHERE(" u.sub_code = #{subCode}");
            }
        }.toString();
    }

    public String update(final Code code) {
        return new SQL() {
            {
                UPDATE("sys_code ");
                if (!Strings.isNullOrEmpty(code.getCataName())) {
                    SET("cata_name = #{cataName}");
                }
                if (!Strings.isNullOrEmpty(code.getStatus())) {
                    SET("status = #{status}");
                }
                if (!Strings.isNullOrEmpty(code.getSubCodeName())) {
                    SET("sub_code_name = #{subCodeName}");
                }

                WHERE("cata_code = #{cataCode}");
                WHERE("sub_code = #{subCode}");
            }
        }.toString();
    }

    public String updateCodeByid(final Code code) {
        return new SQL() {
            {
                UPDATE("sys_code ");
                if (!Strings.isNullOrEmpty(code.getCataCode())) {
                    SET("cata_code = #{cataCode}");
                }
                if (!Strings.isNullOrEmpty(code.getCataName())) {
                    SET("cata_name = #{cataName}");
                }
                if (!Strings.isNullOrEmpty(code.getStatus())) {
                    SET("status = #{status}");
                }
                if (!Strings.isNullOrEmpty(code.getSubCode())) {
                    SET("sub_code = #{subCode}");
                }
                if (!Strings.isNullOrEmpty(code.getSubCodeName())) {
                    SET("sub_code_name = #{subCodeName}");
                }

                WHERE("id = #{id}");
            }
        }.toString();
    }

    public String delete(final Code code) {
        return new SQL() {{
            UPDATE("sys_code");
            SET("status = '0' ");
            WHERE("id = #{id}");
        }}.toString();
    }

    public String deleteCodeBycataCode(final Code code) {
        return new SQL() {{
            UPDATE("sys_code");
            SET("status = '0' ");
            WHERE("cata_code = #{cataCode}");
        }}.toString();
    }

    public String deleteCodeBycataAndSub(final Code code) {
        return new SQL() {{
            UPDATE("sys_code");
            SET("status = '0' ");
            WHERE("cata_code = #{cataCode}");
            if (!Strings.isNullOrEmpty(code.getSubCode())) {
                WHERE("sub_code = #{subCode}");
            }
        }}.toString();
    }
    /**
     * 拼装 getList sql语句
     *
     * @param code
     * @return
     */
    public String getList(final Code code) {
        return getByEntity(code);
    }
}
